<?php

namespace App\Http\Controllers;

use App\Models\WeChatUser;
use Illuminate\Support\Facades\Cache;

class AvatarListController extends Controller
{
    public function index()
    {
        // 随机 PK 头像缓存 1 小时
        $avatars = Cache::remember('avatars', 3600, function () {
            return WeChatUser::query()->inRandomOrder()->limit(100)->pluck('avatar')->toArray();
        });

        return response()->json([
            'code' => 1,
            'message' => 'success',
            'result' => [
                'data' => array_values(
                    array_intersect_key(
                        $avatars,
                        array_flip(array_rand($avatars, 5))
                    )
                ),
            ]
        ]);
    }
}
